package handle_client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientHandler implements Runnable {
	
	private static final int EXIT = 0000;
	private static final int LOGIN = 0001;
	private static final int CHANGEPASSWORD = 0002;
	private static final int CHAT = 0003;
	private static final int SAVEGROUP = 0004;
	private static final int SENDNOTICE = 0005;
	
	private Socket socket;
	private ObjectInputStream input;
	private ObjectOutputStream output;
	private boolean go_on = true;

	public ClientHandler(Socket socket) {
		// TODO Auto-generated constructor stub
		this.socket = socket;
		try {
			output = new ObjectOutputStream(socket.getOutputStream());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			int command = 0;
			while(go_on){
				input = new ObjectInputStream(socket.getInputStream());
				command = input.readInt();
			
				switch(command){
				case EXIT:
					//exit();
					break;
				case LOGIN:
					//input.readObject();
					//System.out.println("someone login");
					break;
				}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	
	private ClientDelegate select_out(int user_number)
	{
		for(ClientDelegate u:ClientDelegate.client_delegates)
			if(u.getUser_number() == user_number)
				return u;
		
		return null;
	}
	
	
	private boolean exit()
	{
		try {
			int user_number = (Integer) input.readObject();
			socket.close();
			ClientDelegate.client_delegates.remove(select_out(user_number));
			System.out.println(user_number+" are out");
			go_on = false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	
	

}

